#include "index.h"

GtkWidget *window_homepage = NULL;
GtkWidget *window_operation = NULL;
GtkWidget *window_showdata = NULL;
GtkWidget *window_modifydata = NULL;
// GtkWidget *window_error = NULL;

void show_window(GtkWidget *widget, gpointer window_name)
{
    // 创建主页面时其他页面还没创建无法显示
    if (strcmp((char *)window_name, "window_homepage") == 0)
        gtk_widget_show_all(window_homepage);
    else if (strcmp((char *)window_name, "window_operation") == 0)
        gtk_widget_show_all(window_operation);
    else if (strcmp((char *)window_name, "window_showdata") == 0)
        gtk_widget_show_all(window_showdata);
    else if (strcmp((char *)window_name, "window_modifydata") == 0)
        gtk_widget_show_all(window_modifydata);
}

void hide_window(GtkWidget *widget, gpointer data)
{
    gtk_widget_hide_all((GtkWidget *)data);
}

void show_info(GtkWidget *window, char *title, gchar *info)
{

    window = create_error_window();
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO,
                                    GTK_BUTTONS_OK,
                                    info);
    gtk_window_set_title(GTK_WINDOW(dialog), title);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_error(GtkWidget *window, char *title, gchar *info)
{
    window = create_error_window();

    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_ERROR,
                                    GTK_BUTTONS_OK,
                                    info);
    gtk_window_set_title(GTK_WINDOW(dialog), title);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_question(GtkWidget *window, char *title, gchar *info)
{
    window = create_error_window();
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_QUESTION,
                                    GTK_BUTTONS_YES_NO,
                                    info);
    gtk_window_set_title(GTK_WINDOW(dialog), title);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_warning(GtkWidget *window, char *title, gchar *info)
{
    window = create_error_window();
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_WARNING,
                                    GTK_BUTTONS_OK,
                                    info);
    gtk_window_set_title(GTK_WINDOW(dialog), title);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

/*--------------------------------数据库写操作--------------------------*/
void db_init()
{
    sqlite3 *db;
    int ret = sqlite3_open("Data.db", &db);
    if (ret != SQLITE_OK)
        printf("db open fail\n");
    else
        printf("db open success\n");

    char *errmsg = NULL;

    ret = sqlite3_exec(db, "create table if not exists persons(id int primary key,name text,addr text,sex text);", NULL, NULL, &errmsg);
    if (ret != SQLITE_OK)
        printf("create table fail:%s\n", errmsg);
    else
        printf("create table success\n");

    ret = sqlite3_close(db);
}
